#include "stdio.h"
#include "string.h"

void enter(char room[21][21],int *room_num,char message[21])
{
	strcpy(room[++(*room_num)],message);	
}

void leave(char room[21][21],int *room_num,char message[21])
{
	int i,j;
	j=0;   /* j is no use ,just be used for check error */
	for (i=1;i<=(*room_num);i++)
	{
		if (strcmp(room[i],message)==0)
		{
			j=i;
			break;
		}	
	}
	if (j==0) printf("Error in sub leave !\n");
	else
	{
		for (i=j;i<=(*room_num);i++)
		{
			strcpy(room[i],room[i+1]);
		}		
		(*room_num)--;
	}
			
}

void addname(char name[21][21],int *name_num,char message[21])
{
	int i,j;
	j=0;
	for (i=1;i<=(*name_num);i++)
	{
		if (strcmp(name[i],message)==0)
		{
			j=1;
			break;
		}
	}
	if (j==0) strcpy(name[++(*name_num)],message);
}

int getmessagesite(char mess[21][21],char message[21],int n)
{
	int i,j;
	for (i=1;i<=n;i++)
	{
		if (strcmp(mess[i],message)==0)
		{
			j=i;
			break;
		}	
	}
	return j;
}

int checkin(char name[21],char room[21][21],int room_num)
{
	int i,j;
	j=0;
	for (i=1;i<=room_num;i++)
	{
		if (strcmp(name,room[i])==0)
		{
			j=1;
			break;
		}
	}
	return j;
}

int checkonerow(int tu[21][21],int row,int n)
{
	int i,j,site;
	j=0;
	for (i=1;i<=n;i++)
	{
		if (tu[row][i]==1)
		{
			j++;
			site=i;
		}
	}	
	if (j==1) return site;
	else return 0;	
}

int checkoneline(int tu[21][21],int line,int n)
{
	int i,j,site;
	j=0;
	for (i=1;i<=n;i++)
	{
		if (tu[i][line]==1)
		{
			j++;
			site=i;
		}
	}	
	if (j==1) return site;
	else return 0;	
}

void point(int tu[21][21],int x,int y,int n)
{
	int i,j;
	for (i=1;i<=n;i++) tu[i][y]=-1;
	for (j=1;j<=n;j++) tu[x][j]=-1;
	tu[x][y]=2;
}
void getorder(char name[21][21],int n)
{
	char s[21];
	int i,j;
	for (i=1;i<n;i++)
		for (j=i+1;j<=n;j++)
		{
			if (strcmp(name[i],name[j])>0)
			{
				strcpy(s,name[i]);
				strcpy(name[i],name[j]);
				strcpy(name[j],s);
			}
		}
}

main()
{
	int tu[21][21];
	char name[21][21],mess[21][21],room[21][21],ordername[21][21];
	char way[2],message[21];
	int num,n,room_num,name_num,message_site,name_site;
	int i,j,k,a,b;
	
	scanf("%d",&num);
	for (k=1;k<=num;k++)
	{
		memset(tu,0,sizeof(tu));
		memset(name,0,sizeof(name)); name_num=0;
		memset(mess,0,sizeof(mess));
		memset(room,0,sizeof(room)); room_num=0;
		memset(ordername,0,sizeof(ordername));
		
		scanf("%d",&n);
		for (i=1;i<=n;i++)
		{
			scanf("%s",mess[i]);
		}
		scanf("%s",way);
		while (way[0]!='Q')
		{
			scanf("%s",message); /*message stands name or message */
			switch (way[0])
			{
				case 'E':enter(room,&room_num,message);
					 addname(name,&name_num,message);
					 break;
				case 'L':leave(room,&room_num,message);break;
				case 'M':
				{
					message_site=getmessagesite(mess,message,n);
					for (i=1;i<=n;i++)
					{
						if (i<=name_num)
						{
							if (checkin(name[i],room,room_num)==1)
							{
								if (tu[i][message_site]==0)
									tu[i][message_site]=1;
							}	
							else
							{
								tu[i][message_site]=-1;
							}
						}
						else
						{
							tu[i][message_site]=-1;
						}
					}
			
				};break;
			} /* the end of switch */


		/*	for (i=1;i<=n;i++)
			{
				for (j=1;j<=n;j++) printf("%2d ",tu[i][j]);
				printf("\n");                              					
			}		
			printf("The room number is :%d\n",room_num);
			printf("\n");*/
				
				
			scanf("%s",way);
		} /* the end of while */
		b=1;
		while (b!=0)
		{
			b=0;/*j is used to check whether the program find only 1 in one row */ 	
			for (i=1;i<=n;i++)
			{
				j=checkonerow(tu,i,n);	
				if (j!=0)
				{
					b=1;
					point(tu,i,j,n);
				}	
			}
			for (i=1;i<=n;i++)
			{
				j=checkoneline(tu,i,n);
				if (j!=0)
				{
					b=1;
					point(tu,j,i,n);
				}
			}
		} /* end of while */
	/*	for (i=1;i<=n;i++)
        		{
        			for (j=1;j<=n;j++) printf("%2d ",tu[i][j]);
        			printf("\n");     
        		}		*/
		for (i=1;i<=n;i++) strcpy(ordername[i],name[i]);
		getorder(ordername,name_num);
		for (i=1;i<=name_num;i++) /* output */
		{
			a=getmessagesite(name,ordername[i],name_num);
			b=0;
			for (j=1;j<=name_num;j++) 
				if (tu[a][j]==2)
				{
					b=j;
					break;
				}
			if (b==0)
				printf("%s:???\n",ordername[i]);
			else 
				printf("%s:%s\n",ordername[i],mess[j]);
		}
		if (k!=num) printf("\n");
	}	
}



